//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.graphreport.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ht.utils.BrowserUtils;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import com.ht.entity.Result;
import com.ht.modules.system.online.graphreport.entity.OnlGraphreportHead;
import com.ht.modules.system.online.graphreport.service.IOnlGraphreportApiService;
import com.ht.modules.system.online.graphreport.service.IOnlGraphreportHeadService;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/online/graphreport/api"})
public class OnlGraphreportApiController {
    @Autowired
    private IOnlGraphreportHeadService headService;
    @Autowired
    private IOnlGraphreportApiService apiService;

    public OnlGraphreportApiController() {
    }

    @GetMapping({"/getTempletAllData"})
    public Result<?> getTempletAllData(@RequestParam(name = "id",required = true) String id) {
        return this.apiService.getTempletChartsData(id);
    }

    @GetMapping({"/getChartsData"})
    public Result<?> getChartsData(@RequestParam(name = "id",required = true) String id, @RequestParam("params") String paramString) {
        OnlGraphreportHead head = (OnlGraphreportHead)this.headService.getById(id);
        return head == null ? Result.error("实体不存在") : Result.ok(this.apiService.queryChartDataSource(head, paramString));
    }

    @GetMapping({"/getChartsDataByCode"})
    public Result<?> getChartsDataByCode(@RequestParam(name = "code",required = true) String code, @RequestParam("params") String paramString) {
        OnlGraphreportHead head = this.headService.queryByCode(code);
        return head == null ? Result.error("实体不存在") : Result.ok(this.apiService.queryChartDataSource(head, paramString));
    }

    @RequestMapping({"/exportXls"})
    public void exportXls(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String name = request.getParameter("name");
        String dataSourceString = URLDecoder.decode(request.getParameter("dataSource"), "UTF-8");
        String entitysString = URLDecoder.decode(request.getParameter("entitys"), "UTF-8");
        if (!StringUtils.isEmpty(dataSourceString) && !StringUtils.isEmpty(entitysString)) {
            if (StringUtils.isEmpty(name)) {
                name = "ExportExecel";
            }

            JSONArray entitysJson = JSONArray.parseArray(entitysString);
            JSONArray dataSourceJson = JSONArray.parseArray(dataSourceString);
            List<ExcelExportEntity> entityList = new ArrayList<>();

            String browse;
            for(int i = 0; i < entitysJson.size(); ++i) {
                JSONObject entity = entitysJson.getJSONObject(i);
                String key = entity.getString("key");
                browse = entity.getString("alias");
                entityList.add(new ExcelExportEntity(browse, key, 30));
            }

            List<Map<String, String>> list = new ArrayList<>();

            for(int i = 0; i < dataSourceJson.size(); ++i) {
                JSONObject data = dataSourceJson.getJSONObject(i);
                Iterator<String> iterator = data.keySet().iterator();
                HashMap map = new HashMap();

                while(iterator.hasNext()) {
                    String key = (String)iterator.next();
                    String value = data.getString(key);
                    map.put(key, value);
                }

                list.add(map);
            }
            ServletOutputStream out = null;
            try {
                Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(name, name), entityList, list);
                browse = BrowserUtils.checkBrowse(request);
                if ("MSIE".equalsIgnoreCase(browse.substring(0, 4))) {
                    response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8") + ".xls");
                } else {
                    String newtitle = new String(name.getBytes("UTF-8"), "ISO8859-1");
                    response.setHeader("content-disposition", "attachment;filename=" + newtitle + ".xls");
                }

                out = response.getOutputStream();
                workbook.write(out);
            } catch (Exception var24) {
            } finally {
                try {
                    out.flush();
                    out.close();
                } catch (Exception var23) {
                }

            }

        } else {
            throw new Exception("缺少必要的参数（dataSource、entitys）");
        }
    }
}
